<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2023-10-29 15:00:17
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Repositories;

use App\Models\CustomerStorageInspect;
use App\Models\CustomerStorageInspectPack;

/**
 * Summary of ProProcessesInspectRepository
 * @property CustomerStorageInspect $model
 * @property CustomerStorageInspectPack $pack
 * @author sunkaiyuan
 */
class CustomerStorageInspectRepository extends Repositories
    {
    public $relation_models = ["status", "creator", "location", "material"];

    public function boot(CustomerStorageInspect $model, CustomerStorageInspectPack $pack)
        {
        $this->model = $model;
        $this->pack  = $pack;
        }
    protected function create($data)
        {

        // 
        // state

        $this->model->create([
            'open_card_code' => $data->code,
            'order_id'       => $data->id,
            'color_name'     => $data->color_name,
            'unit_id'        => $data->unit_id,
            'batchno'        => $data->batchno,
            'remark'         => $data->remark,
            'company_id'     => $data->customer_id,
            'dyelot'         => $data->dyelot,
            'location_id'    => $data->location_id,
            'state'          => 1,
            'material_id'    => $data->material_id,
            'custno'         => $data->custno,
            'cust_order'     => $data->cust_order,
            'width'          => $data->width ?: null
        ]);
        return $data;
        }
    protected function nocreate($data)
        {
        if ($data->inspect->status_id == 1) {
            fail("检验单已审核！");
            }
        $inspec_m = $data->inspect;
        $inspec_m->pack->filter(fn($v) => $v->delete());
        $inspec_m->delete();
        $data->fect_auxiliary_quantity = $data->fect_quantity = 0;
        $data->save();
        }
    }